package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.sysmg.security.model.RoleModel;
import com.e3.web.core.client.GridListener;
import com.e3.web.core.client.ListenerManager;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridSelectionModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;

public class RoleGridPanel extends LayoutContainer{
	
	private GridSelectionModel<RoleModel> gridSelectionModel;
	
	private Grid<RoleModel> roleGrid;
	
	private ListStore<RoleModel> store;
	
	private ListenerManager<RoleModel> listenerManager = new ListenerManager<RoleModel>();
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		this.setLayout(new FitLayout());
		
		this.add(getRoleGrid());
	}
	
	public void addModuleGridListener(GridListener<RoleModel> listener){
		listenerManager.addGridListener(listener);
	}
	
	private Grid<RoleModel> getRoleGrid(){
		if(roleGrid == null){
			ColumnModel cm = new ColumnModel(RoleHelper.INSTANCE.INSTANCE.getColumnConfigs());
			roleGrid = new Grid<RoleModel>(getRoleGridStore(), cm){
				protected void onDoubleClick(GridEvent<RoleModel> e) {
				    onGridRowDoubleClicked(e.getModel());
				}
			};
			
			roleGrid.setSelectionModel(getGridSelectionModel());
		}
		return roleGrid;
	}
	
	public ListStore<RoleModel> getRoleGridStore(){
		if(store == null){
			store = new ListStore<RoleModel>();
			store.setModelComparer(RoleHelper.INSTANCE.getModelComparer());
			store.setKeyProvider(RoleHelper.INSTANCE.getModelKeyProvider());
		}
		return store;
	}
		
	private GridSelectionModel<RoleModel> getGridSelectionModel(){
		if(gridSelectionModel==null){
			gridSelectionModel = new GridSelectionModel<RoleModel>();
			gridSelectionModel.addSelectionChangedListener(new SelectionChangedListener<RoleModel>(){

				@Override
				public void selectionChanged(SelectionChangedEvent<RoleModel> se) {
					RoleModel module = se.getSelectedItem();
					onGridSelectionChanged(module);
				}
			});
			
		}
		return gridSelectionModel;
	}
	
	private void onGridSelectionChanged(RoleModel model) {
		listenerManager.notifyGridSelectChanged(model);
	}
	
	private void onGridRowDoubleClicked(RoleModel model){
		listenerManager.notifyGridRowDoubleClicked(model);
	}
	
	public void setGridValues(List<RoleModel> modules){
		store.removeAll();
		doSetGridValues(modules);
	}
	
	private void doSetGridValues(List<RoleModel> modules){
		if(modules!=null&&modules.size()>0){
			store.add(modules);
			setSelectValue(modules.get(0));
		}
		else{
			onGridSelectionChanged(null);
		}
	}
	
	public void setSelectValue(RoleModel model){
		if(model==null){
			return;
		}
		List<RoleModel> models = new ArrayList<RoleModel>(1);
		models.add(model);
		getGridSelectionModel().setSelection(models);
	}

	public List<RoleModel> getSelectedRoles(){
		return getGridSelectionModel().getSelectedItems();
	}
	
	public void refreshGridData(RoleModel model){
		if(model==null){
			return;
		}
		List<RoleModel> models = store.getModels();
		boolean isUpdate = false;
		if(models!=null&&models.size()>0){
			for (RoleModel roleModel : models) {
				if(roleModel.getId().equals(model.getId())){
					isUpdate = true;
					break;
				}
			}
			if(isUpdate){
				store.update(model);
				setSelectValue(model);
				return;
			}
		}
		List<RoleModel> roles = new ArrayList<RoleModel>(1);
		roles.add(model);
		store.add(roles);
		setSelectValue(model);
	}
	
	public void removeDatas(List<RoleModel> models){
		if(models == null || models.isEmpty()){
			return;
		}
		for (RoleModel roleModel : models) {
			store.remove(roleModel);
		}
	}
	
	public void removeAllData(){
		store.removeAll();
	}
}
